/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pass.jtorrenteditor.helper;

import za.co.pas.jtorrentutils.Utils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.Map;
import za.co.pas.jtorrentparser.parser.FileFormatException;
import za.co.pas.jtorrentparser.parser.IllegalCharacterException;
import za.co.pas.jtorrenttracker.TrackerConnector;
import za.co.pass.jtorrenteditor.list.model.AnnounceListModel;
import za.co.pass.jtorrenteditor.list.model.data.AnnounceData;
import za.co.pass.jtorrenteditor.view.MainFrame;
import org.apache.log4j.Logger;
/**
 *
 * @author alabuschagne
 */
public class CheckThread implements Runnable
{
    private static Logger logger = Logger.getLogger(CheckThread.class);
    private AnnounceData announceData = null;
    private String info_hash = null;
    private boolean busy = false;
    private int listIndex = -1;
    private AnnounceListModel announceListModel = null;
    private MainFrame mainFrame;

    private CheckThread()
    {
    }

    public CheckThread( MainFrame mainFrame, AnnounceListModel announceListModel,
                        int listIndex, AnnounceData announceData, String info_hash)
    {
        setMainFrame(mainFrame);
        setAnnounceListModel(announceListModel);
        setListIndex(listIndex);
        setAnnounceData(announceData);
        setInfo_hash(info_hash);
    }

    private void update()
    {
        if (listIndex == -1)
        {
            mainFrame.set(announceData);
        }
        else
        {
            announceListModel.setAnnounceData(listIndex, announceData);
        }
    }

    public void run()
    {
        if ((announceData == null) || (getInfo_hash() == null))
        {
            return;
        }
        busy = true;
        logger.debug("Thread starting");
        try
        {
            announceData.setBusy(true);
            update();
            TrackerConnector tc = new TrackerConnector();
            tc.setProxy("localhost", 3128, null, null);
            logger.debug("url: " + announceData.getUrl());
//            if(announceData.getUrl().indexOf("pow7.com") != -1)
//            {
//                System.out.println("Stop here!");
//            }
            Map<String, Object> tcMap = tc.getTrackerInfo(announceData.getUrl(), getInfo_hash(), 6881, 0, 0, null);
            if (tcMap == null)
            {
                announceData.setBusy(false);
                announceData.setChecked(false);
                announceData.setOk(false);
                update();
            }
            else
            {
                logger.debug(Utils.debugMap(tcMap, 0));
                announceData.setBusy(false);
                announceData.setChecked(true);
                announceData.setOk(true);
                announceData.setPeers(tc.getPeerIPPort(tcMap));
                update();
            }
        }
        catch(SocketTimeoutException ex)
        {
            logger.error(ex.getMessage(), ex);
            announceData.setBusy(false);
            announceData.setChecked(true);
            announceData.setOk(false);
            announceData.setErrorMsg(ex.getMessage());
            update();
        }
        catch (MalformedURLException ex)
        {
            logger.error(ex.getMessage(), ex);
            announceData.setBusy(false);
            announceData.setChecked(true);
            announceData.setOk(false);
            announceData.setErrorMsg(ex.getMessage());
            update();
        }
        catch (IOException ex)
        {
            logger.error(ex.getMessage(), ex);
            announceData.setBusy(false);
            announceData.setChecked(true);
            announceData.setOk(false);
            announceData.setErrorMsg(ex.getMessage());
            update();
        }
        catch (IllegalCharacterException ex)
        {
            logger.error(ex.getMessage(), ex);
            announceData.setBusy(false);
            announceData.setChecked(true);
            announceData.setOk(false);
            announceData.setErrorMsg(ex.getMessage());
            update();
        }
        catch (FileFormatException ex)
        {
            logger.error(ex.getMessage(), ex);
            announceData.setBusy(false);
            announceData.setChecked(true);
            announceData.setOk(false);
            announceData.setErrorMsg(ex.getMessage());
            update();
        }
        logger.debug("Thread exiting");
        busy = false;
    }

    /**
     * @return the announceData
     */
    public AnnounceData getAnnounceData()
    {
        return announceData;


    }

    /**
     * @param announceData the announceData to set
     */
    public void setAnnounceData(AnnounceData announceData)
    {
        this.announceData = announceData;

    }

    /**
     * @return the info_hash
     */
    public String getInfo_hash()
    {
        return info_hash;
    }

    /**
     * @param info_hash the info_hash to set
     */
    public void setInfo_hash(String info_hash)
    {
        this.info_hash = info_hash;
    }

    public boolean isBusy()
    {
        return busy;
    }

    /**
     * @return the listIndex
     */
    public int getListIndex()
    {
        return listIndex;
    }

    /**
     * @param listIndex the listIndex to set
     */
    public void setListIndex(int listIndex)
    {
        this.listIndex = listIndex;
    }

    /**
     * @return the announceListModel
     */
    public AnnounceListModel getAnnounceListModel()
    {
        return announceListModel;
    }

    /**
     * @param announceListModel the announceListModel to set
     */
    public void setAnnounceListModel(AnnounceListModel announceListModel)
    {
        this.announceListModel = announceListModel;
    }

    /**
     * @return the mainFrame
     */
    public MainFrame getMainFrame()
    {
        return mainFrame;
    }

    /**
     * @param mainFrame the mainFrame to set
     */
    public void setMainFrame(MainFrame mainFrame)
    {
        this.mainFrame = mainFrame;
    }
}
